package com.sinoglobal.sinostore.service;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.bean.BaseVo;
import com.sinoglobal.sinostore.bean.OrderVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
import com.sinoglobal.sinostore.utils.SignUtil;
import com.sinoglobal.sinostore.utils.TextUtil;

public class OrderManager {
	OrderOperationCallBack callBack;
	public void setCallBack(OrderOperationCallBack callBack) {
		this.callBack = callBack;
	}
	public void cancelOrder(final OrderVo orderVo) {
		HttpUtils httpUtils=new HttpUtils();
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "203");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("user_id", Constants.userCenterId);
		params.addQueryStringParameter("pageSize","10");
		params.addQueryStringParameter("order_num",orderVo.getOrder_num());
		String sign=SignUtil.generateSign(params);
		params.addQueryStringParameter("sign",sign);
		ApiDebugUtil.debug(params);
		httpUtils.send(HttpRequest.HttpMethod.POST,
				Constants.STORE_URL,
				params,
				new OrderRequestCallBack() {


			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				if (callBack!=null) {
					callBack.onRequestComplete();
				}
				if (responseInfo!=null&&TextUtil.stringIsNotNull(responseInfo.result)) {
					LogUtils.e("responseInfo.result==>"+responseInfo.result);
					BaseVo baseVo=null;
					try {
						baseVo = JSON.parseObject(responseInfo.result, BaseVo.class);
					} catch (Exception e) {
						e.printStackTrace();
						if (callBack!=null) {
							callBack.onFailure(baseVo);
						}
						return;
					}
					LogUtils.e("code=="+baseVo.getCode());
					if (!"23".equals(baseVo.getCode())) {
						if (callBack!=null) {
							callBack.onFailure(baseVo);
						}
						return;
					}
					orderVo.setStatus("8");
					if (callBack!=null) {
						callBack.onSuccess(baseVo);
					}

				}

			}
		});

		//TODO 请求取消订单接口
	}
	public void deleteOrder(final OrderVo orderVo) {
		HttpUtils httpUtils=new HttpUtils();
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "207");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("user_id", Constants.userCenterId);
		params.addQueryStringParameter("order_num",orderVo.getOrder_num());
		String sign=SignUtil.generateSign(params);
		params.addQueryStringParameter("sign",sign);
		ApiDebugUtil.debug(params);
		httpUtils.send(HttpRequest.HttpMethod.POST,
				Constants.STORE_URL,
				params,
				new OrderRequestCallBack() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				if (callBack!=null) {
					callBack.onRequestComplete();
				}
				if (responseInfo!=null&&TextUtil.stringIsNotNull(responseInfo.result)) {
					LogUtils.e("responseInfo.result==>"+responseInfo.result);
					BaseVo baseVo=null;
					try {
						baseVo = JSON.parseObject(responseInfo.result, BaseVo.class);
					} catch (Exception e) {
						e.printStackTrace();
						if (callBack!=null) {
							callBack.onFailure(baseVo);
						}
						return;
					}
					LogUtils.e("code=="+baseVo.getCode());
					if (!"0".equals(baseVo.getCode())) {
						if (callBack!=null) {
							callBack.onFailure(baseVo);
						}
						return;
					}
					if (callBack!=null) {
						callBack.onSuccess(baseVo);
					}
				}

			}

		});
	}
	/**
	 * 
	 * @author zrh
	 * @methods confirmGoodsReceive 
	 * @description  确认收货
	 * @date 2015年1月16日 下午6:25:31 参数说明
	 */
	public void confirmGoodsReceive(final OrderVo orderVo) {
		HttpUtils httpUtils=new HttpUtils();
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "205");
		params.addQueryStringParameter("user_id", Constants.userCenterId);
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("order_num",orderVo.getOrder_num());
		params.addQueryStringParameter("draw_type", orderVo.getDraw_type());
		String sign=SignUtil.generateSign(params);
		params.addQueryStringParameter("sign",sign);
		ApiDebugUtil.debug(params);
		httpUtils.send(HttpRequest.HttpMethod.POST,
				Constants.STORE_URL,
				params,
				new OrderRequestCallBack() {


			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				if (callBack!=null) {
					callBack.onRequestComplete();
				}
				if (responseInfo!=null&&TextUtil.stringIsNotNull(responseInfo.result)) {
					LogUtils.e("responseInfo.result==>"+responseInfo.result);
					BaseVo baseVo=null;
					try {
						baseVo = JSON.parseObject(responseInfo.result, BaseVo.class);
					} catch (Exception e) {
						e.printStackTrace();
						if (callBack!=null) {
							callBack.onFailure(baseVo);
						}
						return;
					}
					LogUtils.e("code=="+baseVo.getCode());
					if (!"0".equals(baseVo.getCode())) {
						if (callBack!=null) {
							callBack.onFailure(baseVo);
						}
						return;
					}
					orderVo.setStatus("4");//修改订单状态为 已收货，
					if (callBack!=null) {
						callBack.onSuccess(baseVo);
					}
				}
			}
		});

	}
	abstract class OrderRequestCallBack extends RequestCallBack<String>{

		@Override
		public void onFailure(HttpException error, String msg) {
			if (callBack!=null) {
				callBack.onFailure(error,msg);
			}

		}
		@Override
		public void onStart() {
			if (callBack!=null) {
				callBack.onRequestStart();
			}
		}

		//		@Override
		//		public void onLoading(long total, long current, boolean isUploading) {
		//			if (callBack!=null) {
		//				callBack.onLoading(total,current,isUploading);
		//			}
		//		}

	}
	public interface OrderOperationCallBack{
		void onRequestStart();
		//		void onLoading(long total, long current, boolean isUploading);
		void onSuccess(BaseVo baseVo);
		void onRequestComplete();
		void onFailure(HttpException error, String msg);
		void onFailure(BaseVo baseVo);

	}
}
